package com.rebate.web.servlet;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import com.rebate.model.RebateGoods;
import com.rebate.service.IBaseService;
import com.rebate.util.Page;
import com.rebate.util.PageUtils;

@Controller
public class RebateGoodsServlet extends BaseServlet{
	
	private String view = "/goods/listgoods";
	private String createView = "/goods/creategoods";
	private String editView = "/goods/editgoods";
	@Autowired
	private IBaseService<RebateGoods> rebateGoodsService;
	
	/**
	 * 商品列表页面
	 * @throws IOException 
	 */
	@RequestMapping(value = "/goods/index.htm")
	public String index(HttpServletRequest request,ModelMap model,
			HttpServletResponse response,@ModelAttribute("rebateGoods") RebateGoods rebateGoods) throws IOException {
		return view;
	}
	
	/**
	 * 添加商品页面初始化
	 * @throws IOException 
	 */
	@RequestMapping(value = "/goods/add.htm")
	public String addInit(HttpServletRequest request,ModelMap model,
			HttpServletResponse response,@ModelAttribute("rebateGoods") RebateGoods rebateGoods) throws IOException {
		return createView;
	}
	
	/**
	 * 保存商品
	 * @throws IOException 
	 */
	@RequestMapping(value = "/goods/save.htm")
	public void save(HttpServletRequest request,ModelMap model,
			HttpServletResponse response,@ModelAttribute("rebateGoods") RebateGoods rebateGoods) throws IOException {
		Long id = rebateGoodsService.create(rebateGoods);
		if(id != null) {
			response.getWriter().print(1);
		}else {
			response.getWriter().print(0);
		}
	}
	
	/**
	 * 编辑商品页面初始化
	 * @throws IOException 
	 */
	@RequestMapping(value = "/goods/edit.htm")
	public String editInit(HttpServletRequest request,ModelMap model,
			HttpServletResponse response,@ModelAttribute("rebateGoods") RebateGoods rebateGoods) throws IOException {
		rebateGoods = rebateGoodsService.findById(rebateGoods.getId());
		model.put("rebateGoods", rebateGoods);
		return editView;
	}
	
	/**
	 * 更新商品信息
	 * @throws IOException 
	 */
	@RequestMapping(value = "/goods/update.htm")
	public void update(HttpServletRequest request,ModelMap model,
			HttpServletResponse response,@ModelAttribute("rebateGoods") RebateGoods rebateGoods) throws IOException {
		if(rebateGoodsService.update(rebateGoods)) {
			response.getWriter().print(1);
		}else {
			response.getWriter().print(0);
		}
	}
	
	/**
	 * 查询商户List
	 * @throws IOException 
	 */
	@RequestMapping(value = "/goods/list.htm")
	public void getList(HttpServletRequest request,ModelMap model,
			HttpServletResponse response,@ModelAttribute("rebateGoods") RebateGoods rebateGoods) throws IOException {
		Page<RebateGoods> page = PageUtils.getPage(request);
		page = rebateGoodsService.queryForListPage(page, rebateGoods);
		print(request, response, page);
	}
	/**
	 * remove merchanat
	 * @throws IOException 
	 */
	@RequestMapping(value = "/goods/remove.htm")
	public void remove(HttpServletRequest request,ModelMap model,
			HttpServletResponse response,@ModelAttribute("rebateGoods") RebateGoods rebateGoods) throws IOException {
		if(rebateGoodsService.delete(rebateGoods.getId())) {
			response.getWriter().print(1);
		}else {
			response.getWriter().print(0);
		}
	}
}
